package com.changpay.zywy.ui.activity.main

import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.widget.RadioGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.TodayTardAdapter
import com.changpay.zywy.base.BaseMvpActivity
import com.changpay.zywy.mvp.contract.TodayTradContract
import com.changpay.zywy.bean.ProgressBean
import com.changpay.zywy.bean.TodayInfoBean
import com.changpay.zywy.bean.TodayListBean
import com.changpay.zywy.mvp.presenter.TodayTradPresenter
import kotlinx.android.synthetic.main.activity_today_action.*
import kotlinx.android.synthetic.main.title_layout.*

class TodayActionActivity : BaseMvpActivity<TodayTradContract.View, TodayTradContract.Presenter>(),
    TodayTradContract.View {
    private var total_persion: Int = 0
    private var total_group: Int = 0
    val param = hashMapOf<String, String>()
    override fun ShowTodayList(data: TodayListBean) {
        var position = 0
        data.personal_list?.forEach {
            val progress: Int = if (total_persion == 0) {
                100
            } else {
                (it.activation_num?.toDouble()?.div(total_persion))?.times(100)?.toInt()!!
            }
            val progress_data = ProgressBean(progress!!, it.merchant_name!!, it.activation_num!!, position)
            position++
            datas_person.add(progress_data)
        }
        position = 0
        data.group_list?.forEach {

            val progress: Int = if (total_group == 0) {
                100
            } else {
                (it.group_activation_num?.toDouble()?.div(total_group))?.times(100)?.toInt()!!
            }

            val progress_data = ProgressBean(progress!!, it.member_name!!, it.group_activation_num!!, position)
            position++
            datas_group.add(progress_data)
        }
        datas.addAll(datas_person)
        todayTardAdapter.notifyDataSetChanged()

    }

    override fun ShowTodayInfo(data: TodayInfoBean) {
        total_persion = data.personal.toInt()
        total_group = data.group.toInt()
        today_action_title.text = data.total
        today_action_person.text = data.personal
        today_action_group.text = data.group
        mPresenter?.getTodayList(map = param)
    }

    override fun createPresenter(): TodayTradContract.Presenter = TodayTradPresenter()
    override fun attachLayoutRes(): Int = R.layout.activity_today_action

    /**
     * datas_person
     */
    private val datas_person = mutableListOf<ProgressBean>()
    private val datas_group = mutableListOf<ProgressBean>()
    private val datas = mutableListOf<ProgressBean>()

    /**
     * todayTardAdapter
     */
    private val todayTardAdapter: TodayTardAdapter by lazy {
        TodayTardAdapter(this, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(this)
    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas_person.size != 0) {
            val data = datas_person[position]
//            Intent(activity, ContentActivity::class.java).run {
//                putExtra(Constant.CONTENT_URL_KEY, data.link)
//                putExtra(Constant.CONTENT_TITLE_KEY, data.title)
//                putExtra(Constant.CONTENT_ID_KEY, data.id)
//                startActivity(this)
//            }
        }
    }


    override fun initView() {
        super.initView()
        initRecyclerView()
        tv_title.text = "今日激活"
        iv_back.setOnClickListener { finish() }
        today_trad_rg.setOnCheckedChangeListener { group: RadioGroup?, checkedId: Int ->
            when (checkedId) {
                R.id.rb_personal -> {
                    datas.clear()
                    datas.addAll(datas_person)
                    todayTardAdapter.notifyDataSetChanged()
                }

                R.id.rb_group -> {
                    datas.clear()
                    datas.addAll(datas_group)
                    todayTardAdapter.notifyDataSetChanged()
                }
            }
        }

        param["key"] = token
        param["type"] = "2"
    }

    fun initRecyclerView() {
        today_trad_rv.run {
            layoutManager = linearLayoutManager
            adapter = todayTardAdapter
            itemAnimator = DefaultItemAnimator()
        }
        todayTardAdapter.run {
            bindToRecyclerView(today_trad_rv)
            onItemClickListener = this@TodayActionActivity.onItemClickListener
            setEmptyView(R.layout.fragment_empty_layout)
//            addHeaderView(bannerView)
        }
    }


    override fun initData() {

    }

    override fun start() {
        mPresenter?.getTodayInfo(param)
    }


}
